Mapping and geo data science example code¶
Libraries:
- Geopandas
- Plotly
- Folium
Techniques:
- Geocoding
- Reverse geocoding
- Create geodataframe from latitudes and longitudes
- CRS checking and changing
- Reading geojson or shapefiles
- Creating Isochrones
- Measuring distance
- Getting directions
Map styles:
- Basemaps
- Points
- Polygons
- Choropleth
- Heatmap
- Interactivity
import pandas as pd
import matplotlib.pyplot as mpl
import geopandas as gpd
import contextily as ctx
import libpysal as lps
import plotly.express as px
# import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# geo imports
import geopandas as gpd
from mpl_toolkits.axes_grid1 import make_axes_locatable
from shapely.geometry import Point
from geopandas.tools import overlay
# display settings for notebook
%matplotlib inline
pd.set_option('display.max_columns',None)
pd.options.display.max_rows = 999
Geocoding (Nominatim API)¶
There are usage limits that need to be respected. Check the documentation for more info.
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="geoapiExercises")
def get_coordinates(place_name):
location = geolocator.geocode(place_name)
if location:
return location.latitude, location.longitude
else:
return None, None
# Sample data
df1 = pd.DataFrame({'location': ['Reading', 'BD8 0AZ']})
df2 = pd.DataFrame({'location': ['Bangalore', 'Mumbai']})
df1['latitude'], df1['longitude'] = zip(*df1['location'].apply(get_coordinates))
df2['latitude'], df2['longitude'] = zip(*df2['location'].apply(get_coordinates))
df2
| location | latitude | longitude | |
|---|---|---|---|
| 0 | Bangalore | 12.976794 | 77.590082 |
| 1 | Mumbai | 19.081577 | 72.886628 |
Create geodataframe¶
A geodataframe is like a pandas dataframe with a special geometry column. The geometry column can contain points, polygons or multipolygons (multiple polygons e.g., to describe an town in two parts with a river splitting the shape into two polygons).
If you have longitude and latitude these can be combined to form a 'Point' data type.
In geopandas the active geometry column is automatically used to perform geo operations and plotting.
# convert pandas dataframe to geodataframe
gdf1 = gpd.GeoDataFrame(df1)
print(type(gdf1))
# create a special 'geometry' column containing points from latitudes and longitudes
# This will be your active geometry column
gdf1['geometry'] = gdf1.apply(lambda row: Point(row['longitude'], row['latitude']), axis=1)
# Check the name of the active geometry column
print(gdf1.geometry.name)
#################################
# Another option is to transform the lat and long using a geopandas function
# gdf1 = gpd.GeoDataFrame(ldn_pcs, geometry=gpd.points_from_xy(df1['longitude'], df1['latitude']), crs = 'EPSG:27700')
#################################
<class 'geopandas.geodataframe.GeoDataFrame'> geometry
/Users/rachelcooper/opt/anaconda3/lib/python3.9/site-packages/pandas/core/dtypes/cast.py:122: ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0. Convert the '.coords' to a numpy array instead. arr = construct_1d_object_array_from_listlike(values)
Check and set CRS¶
The Coordinate Reference System (CRS) is used to provide context for the coordinates. It tells you where and how a point on a map corresponds to a location on the Earth. Latitudes and longitudes are a geometric CRS. To plot our points on a flat surface we need a projected CRS
# common CRS for UK maps:
gdf1.crs = "EPSG:27700"
# gdf1.to_crs = ('EPSG:27700')
gdf1.crs
<Derived Projected CRS: EPSG:27700> Name: OSGB36 / British National Grid Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore. - bounds: (-9.0, 49.75, 2.01, 61.01) Coordinate Operation: - name: British National Grid - method: Transverse Mercator Datum: Ordnance Survey of Great Britain 1936 - Ellipsoid: Airy 1830 - Prime Meridian: Greenwich
Depending on the extent of your map and the purpose you might need to use a different crs of which there are many to choose from. Note: to change the crs you will need to reproject to the new crs. The crs won't change by calling something like the cell above.
Chosing the appropiate crs and checking that all your data is using the same one is a good way to debug problems with data appearing in the wrong location on the map.
reprojected_gdf = gdf1.to_crs("EPSG:4326")
reprojected_gdf.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
Reading shapefiles¶
Shapefiles have a column with geometry. The shapefile has the extension .shp and this is the one we will read in. However, the other files which come with the .shp need to be kept in the same directory.
# change this to your own
path = '/Users/rachelcooper/Downloads/IMD_2019 (1)/'
imd = gpd.read_file(path + 'IMD_2019.shp')
imd.head(3)
| lsoa11cd | lsoa11nm | lsoa11nmw | st_areasha | st_lengths | IMD_Rank | IMD_Decile | LSOA01NM | LADcd | LADnm | IMDScore | IMDRank0 | IMDDec0 | IncScore | IncRank | IncDec | EmpScore | EmpRank | EmpDec | EduScore | EduRank | EduDec | HDDScore | HDDRank | HDDDec | CriScore | CriRank | CriDec | BHSScore | BHSRank | BHSDec | EnvScore | EnvRank | EnvDec | IDCScore | IDCRank | IDCDec | IDOScore | IDORank | IDODec | CYPScore | CYPRank | CYPDec | ASScore | ASRank | ASDec | GBScore | GBRank | GBDec | WBScore | WBRank | WBDec | IndScore | IndRank | IndDec | OutScore | OutRank | OutDec | TotPop | DepChi | Pop16_59 | Pop60+ | WorkPop | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | E01000001 | City of London 001A | City of London 001A | 133320.768872 | 2291.846072 | 29199 | 9 | City of London 001A | E09000001 | City of London | 6.208 | 29199 | 9 | 0.007 | 32831 | 10 | 0.010 | 32742 | 10 | 0.024 | 32842 | 10 | -1.654 | 32113 | 10 | -2.012 | 32662 | 10 | 29.472 | 7319 | 3 | 31.873 | 7789 | 3 | 0.006 | 32806 | 10 | 0.012 | 32820 | 10 | -2.107 | 32777 | 10 | 0.032 | 32843 | 10 | -0.430 | 22985 | 7 | 3.587 | 3216 | 1 | 0.006 | 16364 | 5 | 1.503 | 1615 | 1 | 1296 | 175 | 656 | 465 | 715.00 | POLYGON ((532105.092 182011.230, 532162.491 18... |
| 1 | E01000002 | City of London 001B | City of London 001B | 226191.272990 | 2433.960112 | 30379 | 10 | City of London 001B | E09000001 | City of London | 5.143 | 30379 | 10 | 0.034 | 29901 | 10 | 0.027 | 31190 | 10 | 0.063 | 32832 | 10 | -1.115 | 29705 | 10 | -2.343 | 32789 | 10 | 24.412 | 11707 | 4 | 23.084 | 13070 | 4 | 0.037 | 29682 | 10 | 0.030 | 31938 | 10 | -1.907 | 32666 | 10 | 0.034 | 32841 | 10 | -1.060 | 30223 | 10 | 3.231 | 3894 | 2 | -0.410 | 22676 | 7 | 1.196 | 2969 | 1 | 1156 | 182 | 580 | 394 | 619.75 | POLYGON ((532746.813 181786.891, 532671.688 18... |
| 2 | E01000003 | City of London 001C | City of London 001C | 57302.966538 | 1142.359799 | 14915 | 5 | City of London 001C | E09000001 | City of London | 19.402 | 14915 | 5 | 0.086 | 18510 | 6 | 0.086 | 15103 | 5 | 5.804 | 26386 | 9 | -0.102 | 17600 | 6 | -1.032 | 29363 | 9 | 40.103 | 2157 | 1 | 40.535 | 4092 | 2 | 0.052 | 27063 | 9 | 0.128 | 16377 | 5 | -0.292 | 20837 | 7 | 0.142 | 30999 | 10 | -0.691 | 26719 | 9 | 5.173 | 818 | 1 | -0.054 | 17318 | 6 | 2.207 | 162 | 1 | 1350 | 146 | 759 | 445 | 804.00 | POLYGON ((532135.145 182198.119, 532158.250 18... |
imd.crs
<Derived Projected CRS: EPSG:27700> Name: OSGB36 / British National Grid Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore. - bounds: (-9.0, 49.75, 2.01, 61.01) Coordinate Operation: - name: British National Grid - method: Transverse Mercator Datum: Ordnance Survey of Great Britain 1936 - Ellipsoid: Airy 1830 - Prime Meridian: Greenwich
Plot a choropleth using Geopandas¶
Download and import a basemap to plot onto¶
UK map shapefile including N Ireland, Scotland and Wales¶
path = '/Users/rachelcooper/Library/CloudStorage/OneDrive-SMRS/Beds/'
UK_shape = gpd.read_file(path + 'infuse_ctry_2011/infuse_ctry_2011.shp') # crs = 'EPSG:27700'
UK_shape.crs
<Derived Projected CRS: EPSG:27700> Name: OSGB36 / British National Grid Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore. - bounds: (-9.0, 49.75, 2.01, 61.01) Coordinate Operation: - name: British National Grid - method: Transverse Mercator Datum: Ordnance Survey of Great Britain 1936 - Ellipsoid: Airy 1830 - Prime Meridian: Greenwich
# plot
var = 'IMD_Decile'
fig = plt.figure(figsize = (30,30))
ax = fig.add_subplot(121)
divider = make_axes_locatable(ax)
base = UK_shape.plot(ax=ax, color = 'beige')
minx, miny, maxx, maxy = UK_shape.total_bounds
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
cax = divider.append_axes("right", size="5%", pad=0.1)
imd.plot(ax = base,
column = var,
cmap = 'RdBu',
cax = cax,
vmin=imd[var].min(), vmax=imd[var].max(),
legend = True,
legend_kwds={'label': var},
alpha = .5)
ax.axis('off')
# Add title
ax.set_title('Example choropleth map', fontdict={'fontsize': 20}, loc='center')
plt.savefig(path + 'Example_Choropleth.png', dpi=300, bbox_inches='tight')
The contextily library has some nicer looking basemaps to use.¶
# pip install contextily
import contextily as ctx
imd_reprojected = imd.to_crs(epsg=3857)
imd_reprojected.crs
<Derived Projected CRS: EPSG:3857> Name: WGS 84 / Pseudo-Mercator Axis Info [cartesian]: - X[east]: Easting (metre) - Y[north]: Northing (metre) Area of Use: - name: World between 85.06°S and 85.06°N. - bounds: (-180.0, -85.06, 180.0, 85.06) Coordinate Operation: - name: Popular Visualisation Pseudo-Mercator - method: Popular Visualisation Pseudo Mercator Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
# plot
var = 'IMD_Decile'
fig = plt.figure(figsize = (30,30))
ax = fig.add_subplot(121)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.1)
imd_reprojected.plot(ax = ax, # changed the ax to ax and not the uk basemap we used previously.
column = var,
cmap = 'RdBu',
cax = cax,
vmin=imd_reprojected[var].min(), vmax=imd_reprojected[var].max(),
legend = True,
legend_kwds={'label': var},
alpha = .5)
ax.axis('off')
# Add title
ax.set_title('Example choropleth map', fontdict={'fontsize': 20}, loc='center')
# add the contextily basemap
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik)
# There are a choices of contextily basemaps e.g.,
# source=ctx.providers.OpenStreetMap.Mapnik
plt.savefig(path + 'Example_Choropleth.png', dpi=300, bbox_inches='tight')
/Users/rachelcooper/opt/anaconda3/lib/python3.9/site-packages/contextily/tile.py:581: UserWarning: The inferred zoom level of 24 is not valid for the current tile provider (valid zooms: 0 - 19). warnings.warn(msg)
Plot points¶
Get some more points to play with¶
gdf_lnd_postcodes = gpd.read_file('/Users/rachelcooper/Library/CloudStorage/OneDrive-SMRS/London_60mins_postcodes.shp')
gdf_lnd_postcodes.head()
| id | postcode | latitude | longitude | geometry | |
|---|---|---|---|---|---|
| 0 | 69025 | WD7 9EU | 51.694131 | -0.292763 | POINT (-0.29276 51.69413) |
| 1 | 69026 | WD7 9EW | 51.684390 | -0.272313 | POINT (-0.27231 51.68439) |
| 2 | 69027 | WD7 9EX | 51.693555 | -0.292090 | POINT (-0.29209 51.69356) |
| 3 | 69028 | WD7 9EY | 51.694202 | -0.291487 | POINT (-0.29149 51.69420) |
| 4 | 69029 | WD7 9EZ | 51.692161 | -0.292099 | POINT (-0.29210 51.69216) |
gdf_lnd_postcodes.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
# plot points
fig = plt.figure(figsize = (30,30))
ax = fig.add_subplot(121)
divider = make_axes_locatable(ax)
minx, miny, maxx, maxy = gdf_lnd_postcodes.total_bounds
# add a buffer around the map extent to give room for labels
x_buffer = (maxx - minx) * 0.08
y_buffer = (maxy - miny) * 0.08
minx -= x_buffer
miny -= y_buffer
maxx += x_buffer
maxy += y_buffer
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
gdf_lnd_postcodes.plot(
ax = ax,
color = 'teal',
marker='o',
markersize = 10,
alpha = .01)
ax.axis('off')
# Add title
ax.set_title('Example points map', fontdict={'fontsize': 20}, loc='center')
# add the contextily basemap
ctx.add_basemap(ax, source = ctx.providers.CartoDB.Positron, crs=gdf_lnd_postcodes.crs, zoom=8)
# get a random subset to highlight
gdf_sampled = gdf_lnd_postcodes.sample(3)
# highlight some other points and annotate
from adjustText import adjust_text
offset = 0.02 # Adjust this value based on your map's extent and desired label positioning
texts = []
for i, point in gdf_sampled.iterrows():
texts.append(plt.text(point.geometry.x + offset, point.geometry.y, f'Campus {i}', fontsize=12,
bbox=dict(facecolor='white', edgecolor='white', boxstyle='round,pad=0.5', alpha=0.7)))
adjust_text(texts)
gdf_sampled.plot(
ax = ax,
color = 'white',
marker='*',
markersize = 160,
alpha = 1,
edgecolor = 'red')
plt.savefig(path + 'Example_points_map.png', dpi=300, bbox_inches='tight')
Spatial joins¶
Perform a spatial join to show which points are 'contained' in each local authority area in the IMD dataset.¶
There are other options for sjoins e.g., 'within' or 'intersects'. You can also add a buffer around each area before the join or find the nearest neighbour. See here¶
imd_2 = imd.to_crs(gdf_lnd_postcodes.crs)
imd_2.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
# Perform the spatial join
joined_gdf = gpd.sjoin(gdf_lnd_postcodes, imd_2, how="inner", predicate="within")
# Check a couple of points are contained in a polygon
print(any(imd_2.contains(gdf_lnd_postcodes.geometry.iloc[0])))
print(any(imd_2.contains(gdf_lnd_postcodes.geometry.iloc[1])))
True True
joined_gdf.head()
| id | postcode | latitude | longitude | geometry | index_right | lsoa11cd | lsoa11nm | lsoa11nmw | st_areasha | st_lengths | IMD_Rank | IMD_Decile | LSOA01NM | LADcd | LADnm | IMDScore | IMDRank0 | IMDDec0 | IncScore | IncRank | IncDec | EmpScore | EmpRank | EmpDec | EduScore | EduRank | EduDec | HDDScore | HDDRank | HDDDec | CriScore | CriRank | CriDec | BHSScore | BHSRank | BHSDec | EnvScore | EnvRank | EnvDec | IDCScore | IDCRank | IDCDec | IDOScore | IDORank | IDODec | CYPScore | CYPRank | CYPDec | ASScore | ASRank | ASDec | GBScore | GBRank | GBDec | WBScore | WBRank | WBDec | IndScore | IndRank | IndDec | OutScore | OutRank | OutDec | TotPop | DepChi | Pop16_59 | Pop60+ | WorkPop | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 69025 | WD7 9EU | 51.694131 | -0.292763 | POINT (-0.29276 51.69413) | 22954 | E01023582 | Hertsmere 004B | Hertsmere 004B | 3.030249e+06 | 14037.057506 | 18178 | 6 | Hertsmere 004B | E07000098 | Hertsmere | 15.768 | 18178 | 6 | 0.094 | 17331 | 6 | 0.072 | 18348 | 6 | 12.316 | 19648 | 6 | -0.362 | 21610 | 7 | 0.128 | 14685 | 5 | 35.192 | 3993 | 2 | 10.538 | 23498 | 8 | 0.129 | 16099 | 5 | 0.096 | 20606 | 7 | -0.131 | 18269 | 6 | 0.266 | 19861 | 7 | 0.912 | 4024 | 2 | 0.836 | 11226 | 4 | -0.637 | 25614 | 8 | 0.12 | 13311 | 5 | 1712 | 396 | 956 | 360 | 954.25 |
| 5 | 69030 | WD7 9GA | 51.699104 | -0.288466 | POINT (-0.28847 51.69910) | 22954 | E01023582 | Hertsmere 004B | Hertsmere 004B | 3.030249e+06 | 14037.057506 | 18178 | 6 | Hertsmere 004B | E07000098 | Hertsmere | 15.768 | 18178 | 6 | 0.094 | 17331 | 6 | 0.072 | 18348 | 6 | 12.316 | 19648 | 6 | -0.362 | 21610 | 7 | 0.128 | 14685 | 5 | 35.192 | 3993 | 2 | 10.538 | 23498 | 8 | 0.129 | 16099 | 5 | 0.096 | 20606 | 7 | -0.131 | 18269 | 6 | 0.266 | 19861 | 7 | 0.912 | 4024 | 2 | 0.836 | 11226 | 4 | -0.637 | 25614 | 8 | 0.12 | 13311 | 5 | 1712 | 396 | 956 | 360 | 954.25 |
| 14 | 69127 | WD7 9HT | 51.697273 | -0.290561 | POINT (-0.29056 51.69727) | 22954 | E01023582 | Hertsmere 004B | Hertsmere 004B | 3.030249e+06 | 14037.057506 | 18178 | 6 | Hertsmere 004B | E07000098 | Hertsmere | 15.768 | 18178 | 6 | 0.094 | 17331 | 6 | 0.072 | 18348 | 6 | 12.316 | 19648 | 6 | -0.362 | 21610 | 7 | 0.128 | 14685 | 5 | 35.192 | 3993 | 2 | 10.538 | 23498 | 8 | 0.129 | 16099 | 5 | 0.096 | 20606 | 7 | -0.131 | 18269 | 6 | 0.266 | 19861 | 7 | 0.912 | 4024 | 2 | 0.836 | 11226 | 4 | -0.637 | 25614 | 8 | 0.12 | 13311 | 5 | 1712 | 396 | 956 | 360 | 954.25 |
| 15 | 69128 | WD7 9HU | 51.697048 | -0.289933 | POINT (-0.28993 51.69705) | 22954 | E01023582 | Hertsmere 004B | Hertsmere 004B | 3.030249e+06 | 14037.057506 | 18178 | 6 | Hertsmere 004B | E07000098 | Hertsmere | 15.768 | 18178 | 6 | 0.094 | 17331 | 6 | 0.072 | 18348 | 6 | 12.316 | 19648 | 6 | -0.362 | 21610 | 7 | 0.128 | 14685 | 5 | 35.192 | 3993 | 2 | 10.538 | 23498 | 8 | 0.129 | 16099 | 5 | 0.096 | 20606 | 7 | -0.131 | 18269 | 6 | 0.266 | 19861 | 7 | 0.912 | 4024 | 2 | 0.836 | 11226 | 4 | -0.637 | 25614 | 8 | 0.12 | 13311 | 5 | 1712 | 396 | 956 | 360 | 954.25 |
| 17 | 69130 | WD7 9HX | 51.696844 | -0.291330 | POINT (-0.29133 51.69684) | 22954 | E01023582 | Hertsmere 004B | Hertsmere 004B | 3.030249e+06 | 14037.057506 | 18178 | 6 | Hertsmere 004B | E07000098 | Hertsmere | 15.768 | 18178 | 6 | 0.094 | 17331 | 6 | 0.072 | 18348 | 6 | 12.316 | 19648 | 6 | -0.362 | 21610 | 7 | 0.128 | 14685 | 5 | 35.192 | 3993 | 2 | 10.538 | 23498 | 8 | 0.129 | 16099 | 5 | 0.096 | 20606 | 7 | -0.131 | 18269 | 6 | 0.266 | 19861 | 7 | 0.912 | 4024 | 2 | 0.836 | 11226 | 4 | -0.637 | 25614 | 8 | 0.12 | 13311 | 5 | 1712 | 396 | 956 | 360 | 954.25 |
joined_gdf.shape
(243119, 69)
joined_gdf = joined_gdf.to_crs(epsg = 4326)
joined_gdf.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
# plot points
var = 'IncDec'
fig = plt.figure(figsize = (30,30))
ax = fig.add_subplot(121)
divider = make_axes_locatable(ax)
minx, miny, maxx, maxy = joined_gdf.total_bounds
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
cax = divider.append_axes("right", size="5%", pad=0.1)
joined_gdf.plot(
ax = ax,
legend = True,
cax = cax,
legend_kwds={'label': var},
cmap = 'cividis_r',
marker='o',
markersize = 10,
alpha = .4,
column = var)
ax.axis('off')
# Add title
ax.set_title('Example points map', fontdict={'fontsize': 20}, loc='center')
# add the contextily basemap
ctx.add_basemap(ax, source = ctx.providers.CartoDB.Positron, crs=joined_gdf.crs, zoom=8)
# plt.savefig(path + 'Example_points_map.png', dpi=300, bbox_inches='tight')
Interactive maps using Folium¶
# see Yodel map code
Getting Drive Distance (based on route, not as the crow flies or and the worm burrows) and Creating Isochones¶
start_long = -0.969651
start_lat = 51.456659
end_long = -1.796500
end_lat = 53.800910
import openrouteservice
from shapely.geometry import shape
# enter your API key for open route service
# my_key = ''
# Create a client instance
client = openrouteservice.Client(key=my_key)
# Define coordinates for start and end points
coords = ((start_long, start_lat), (end_long, end_lat))
# Calculate driving route
route = client.directions(coordinates=coords, profile='driving-car', format='geojson')
# Extract distance from the route
distance = route['features'][0]['properties']['segments'][0]['distance']
duration = route['features'][0]['properties']['segments'][0]['duration']
print(f"Driving distance: {distance/1000} km \nDriving time: {duration/3600} hours")
Driving distance: 328.6921 km Driving time: 3.76275 hours
# You can have a look at the route info below
route
{'type': 'FeatureCollection',
'metadata': {'attribution': 'openrouteservice.org | OpenStreetMap contributors',
'service': 'routing',
'timestamp': 1692298995101,
'query': {'coordinates': [[-0.969651, 51.456659], [-1.7965, 53.80091]],
'profile': 'driving-car',
'format': 'geojson'},
'engine': {'version': '7.1.0',
'build_date': '2023-07-09T01:31:50Z',
'graph_date': '2023-08-13T10:08:47Z'}},
'bbox': [-1.796394, 51.456589, -0.937551, 53.801036],
'features': [{'bbox': [-1.796394, 51.456589, -0.937551, 53.801036],
'type': 'Feature',
'properties': {'transfers': 0,
'fare': 0,
'segments': [{'distance': 328692.1,
'duration': 13545.9,
'steps': [{'distance': 144.0,
'duration': 20.7,
'type': 11,
'instruction': 'Head south on Blagrave Street',
'name': 'Blagrave Street',
'way_points': [0, 7]},
{'distance': 239.9,
'duration': 62.2,
'type': 1,
'instruction': 'Turn right onto Station Road',
'name': 'Station Road',
'way_points': [7, 17]},
{'distance': 132.8,
'duration': 28.6,
'type': 0,
'instruction': 'Turn left onto Forbury Road',
'name': 'Forbury Road',
'way_points': [17, 23]},
{'distance': 170.4,
'duration': 36.3,
'type': 7,
'instruction': 'Enter the roundabout and take the 1st exit onto Vastern Road, A329',
'name': 'Vastern Road, A329',
'exit_number': 1,
'way_points': [23, 31]},
{'distance': 767.6,
'duration': 101.3,
'type': 7,
'instruction': 'Enter the roundabout and take the 3rd exit onto George Street, B3345',
'name': 'George Street, B3345',
'exit_number': 3,
'way_points': [31, 53]},
{'distance': 49.1,
'duration': 8.8,
'type': 12,
'instruction': 'Keep left',
'name': '-',
'way_points': [53, 55]},
{'distance': 253.5,
'duration': 28.3,
'type': 4,
'instruction': 'Turn slight left onto Gosbrook Road',
'name': 'Gosbrook Road',
'way_points': [55, 61]},
{'distance': 1525.3,
'duration': 137.3,
'type': 1,
'instruction': 'Turn right onto Westfield Road',
'name': 'Westfield Road',
'way_points': [61, 91]},
{'distance': 11673.4,
'duration': 846.2,
'type': 7,
'instruction': 'Enter the roundabout and take the 2nd exit onto Buckingham Drive, B481',
'name': 'Buckingham Drive, B481',
'exit_number': 2,
'way_points': [91, 296]},
{'distance': 310.8,
'duration': 17.9,
'type': 7,
'instruction': 'Enter the roundabout and take the 1st exit onto A4130',
'name': 'A4130',
'exit_number': 1,
'way_points': [296, 304]},
{'distance': 8451.6,
'duration': 608.3,
'type': 1,
'instruction': 'Turn right onto Watlington Street, B481',
'name': 'Watlington Street, B481',
'way_points': [304, 472]},
{'distance': 243.2,
'duration': 21.9,
'type': 1,
'instruction': 'Turn right onto Couching Street, B4009',
'name': 'Couching Street, B4009',
'way_points': [472, 480]},
{'distance': 4314.6,
'duration': 289.6,
'type': 1,
'instruction': 'Turn right onto Shirburn Street, B4009',
'name': 'Shirburn Street, B4009',
'way_points': [480, 543]},
{'distance': 10599.4,
'duration': 381.6,
'type': 12,
'instruction': 'Keep left',
'name': '-',
'way_points': [543, 611]},
{'distance': 28257.5,
'duration': 1017.3,
'type': 13,
'instruction': 'Keep right onto M40',
'name': 'M40',
'way_points': [611, 791]},
{'distance': 652.6,
'duration': 30.2,
'type': 12,
'instruction': 'Keep left onto M40',
'name': 'M40',
'way_points': [791, 802]},
{'distance': 1415.5,
'duration': 88.6,
'type': 7,
'instruction': 'Enter the roundabout and take the 2nd exit onto A43',
'name': 'A43',
'exit_number': 2,
'way_points': [802, 844]},
{'distance': 5222.8,
'duration': 191.5,
'type': 7,
'instruction': 'Enter the roundabout and take the 2nd exit onto A43',
'name': 'A43',
'exit_number': 2,
'way_points': [844, 887]},
{'distance': 1851.2,
'duration': 68.9,
'type': 7,
'instruction': 'Enter the roundabout and take the 2nd exit onto A43',
'name': 'A43',
'exit_number': 2,
'way_points': [887, 917]},
{'distance': 1149.6,
'duration': 41.4,
'type': 7,
'instruction': 'Enter the roundabout and take the 2nd exit onto A43',
'name': 'A43',
'exit_number': 2,
'way_points': [917, 932]},
{'distance': 1343.4,
'duration': 48.4,
'type': 7,
'instruction': 'Enter the roundabout and take the 3rd exit onto A43',
'name': 'A43',
'exit_number': 3,
'way_points': [932, 960]},
{'distance': 1758.5,
'duration': 63.3,
'type': 7,
'instruction': 'Enter the roundabout and take the 2nd exit onto Brackley Bypass, A43',
'name': 'Brackley Bypass, A43',
'exit_number': 2,
'way_points': [960, 981]},
{'distance': 14830.7,
'duration': 561.6,
'type': 7,
'instruction': 'Enter the roundabout and take the 3rd exit onto A43',
'name': 'A43',
'exit_number': 3,
'way_points': [981, 1127]},
{'distance': 1345.1,
'duration': 69.3,
'type': 7,
'instruction': 'Enter the roundabout and take the 2nd exit onto A43',
'name': 'A43',
'exit_number': 2,
'way_points': [1127, 1152]},
{'distance': 8917.7,
'duration': 321.3,
'type': 7,
'instruction': 'Enter the roundabout and take the 3rd exit onto A43',
'name': 'A43',
'exit_number': 3,
'way_points': [1152, 1235]},
{'distance': 336.3,
'duration': 14.2,
'type': 7,
'instruction': 'Enter the roundabout and take the 1st exit onto A43',
'name': 'A43',
'exit_number': 1,
'way_points': [1235, 1249]},
{'distance': 19790.1,
'duration': 730.2,
'type': 6,
'instruction': 'Continue straight onto A43',
'name': 'A43',
'way_points': [1249, 1350]},
{'distance': 35757.7,
'duration': 1287.2,
'type': 13,
'instruction': 'Keep right onto M1',
'name': 'M1',
'way_points': [1350, 1563]},
{'distance': 91280.5,
'duration': 3286.0,
'type': 13,
'instruction': 'Keep right onto M1',
'name': 'M1',
'way_points': [1563, 2260]},
{'distance': 50171.4,
'duration': 1806.1,
'type': 6,
'instruction': 'Continue straight onto M1',
'name': 'M1',
'way_points': [2260, 2651]},
{'distance': 449.8,
'duration': 16.2,
'type': 12,
'instruction': 'Keep left',
'name': '-',
'way_points': [2651, 2656]},
{'distance': 15004.1,
'duration': 540.1,
'type': 12,
'instruction': 'Keep left',
'name': '-',
'way_points': [2656, 2789]},
{'distance': 669.4,
'duration': 24.1,
'type': 12,
'instruction': 'Keep left',
'name': '-',
'way_points': [2789, 2797]},
{'distance': 3972.1,
'duration': 210.1,
'type': 7,
'instruction': 'Enter the roundabout and take the 6th exit',
'name': '-',
'exit_number': 6,
'way_points': [2797, 2854]},
{'distance': 347.8,
'duration': 12.5,
'type': 12,
'instruction': 'Keep left onto M606',
'name': 'M606',
'way_points': [2854, 2859]},
{'distance': 2845.0,
'duration': 304.9,
'type': 7,
'instruction': 'Enter the roundabout and take the 3rd exit onto Rooley Lane, A6177',
'name': 'Rooley Lane, A6177',
'exit_number': 3,
'way_points': [2859, 2933]},
{'distance': 2100.3,
'duration': 191.9,
'type': 6,
'instruction': 'Continue straight onto Beckside Road',
'name': 'Beckside Road',
'way_points': [2933, 2994]},
{'distance': 9.6,
'duration': 0.8,
'type': 0,
'instruction': 'Turn left onto Thornton Road, B6145',
'name': 'Thornton Road, B6145',
'way_points': [2994, 2995]},
{'distance': 338.1,
'duration': 30.4,
'type': 1,
'instruction': 'Turn right onto Allerton Road',
'name': 'Allerton Road',
'way_points': [2995, 3001]},
{'distance': 0.0,
'duration': 0.0,
'type': 10,
'instruction': 'Arrive at Allerton Road, on the left',
'name': '-',
'way_points': [3001, 3001]}]}],
'way_points': [0, 3001],
'summary': {'distance': 328692.1, 'duration': 13545.9}},
'geometry': {'coordinates': [[-0.970155, 51.456731],
[-0.970171, 51.456687],
[-0.970223, 51.456645],
[-0.970325, 51.456601],
[-0.970476, 51.456589],
[-0.971068, 51.456678],
[-0.971413, 51.456725],
[-0.972093, 51.456749],
[-0.972154, 51.457601],
[-0.972176, 51.457871],
[-0.972131, 51.457953],
[-0.972069, 51.457968],
[-0.971869, 51.457959],
[-0.971503, 51.457936],
[-0.971117, 51.457929],
[-0.970829, 51.457943],
[-0.970728, 51.457946],
[-0.970628, 51.457919],
[-0.970585, 51.457939],
[-0.970496, 51.457949],
[-0.970185, 51.457939],
[-0.969353, 51.457913],
[-0.969075, 51.457906],
[-0.968727, 51.457929],
[-0.96869, 51.457972],
[-0.968687, 51.45808],
[-0.968644, 51.45827],
[-0.968633, 51.45855],
[-0.968549, 51.45911],
[-0.968545, 51.459149],
[-0.968598, 51.459269],
[-0.968771, 51.459411],
[-0.968899, 51.459453],
[-0.968991, 51.459518],
[-0.969046, 51.459615],
[-0.969042, 51.459692],
[-0.968948, 51.459809],
[-0.968862, 51.459854],
[-0.968755, 51.459882],
[-0.968652, 51.459887],
[-0.968388, 51.460125],
[-0.968262, 51.460393],
[-0.967645, 51.461472],
[-0.96751, 51.461717],
[-0.96688, 51.462507],
[-0.966137, 51.463292],
[-0.966018, 51.463671],
[-0.96602, 51.463917],
[-0.966021, 51.464272],
[-0.966041, 51.464691],
[-0.966043, 51.46471],
[-0.966052, 51.465262],
[-0.966077, 51.465658],
[-0.966112, 51.465724],
[-0.966304, 51.465902],
[-0.966574, 51.466054],
[-0.966857, 51.466097],
[-0.967274, 51.46616],
[-0.96841, 51.466291],
[-0.968784, 51.466361],
[-0.969718, 51.466577],
[-0.970084, 51.466674],
[-0.970108, 51.466772],
[-0.970034, 51.467683],
[-0.969895, 51.469316],
[-0.969983, 51.469407],
[-0.970046, 51.469484],
[-0.970025, 51.469534],
[-0.969697, 51.469955],
[-0.969512, 51.47031],
[-0.969097, 51.471356],
[-0.968742, 51.472225],
[-0.968554, 51.472616],
[-0.968453, 51.472782],
[-0.968261, 51.473076],
[-0.96796, 51.473535],
[-0.967771, 51.473885],
[-0.967502, 51.474434],
[-0.967278, 51.474893],
[-0.967048, 51.475312],
[-0.967013, 51.475451],
[-0.967043, 51.475868],
[-0.967022, 51.476067],
[-0.966944, 51.476306],
[-0.966854, 51.476436],
[-0.966499, 51.476944],
[-0.966351, 51.477164],
[-0.965767, 51.478075],
[-0.965434, 51.47868],
[-0.965319, 51.478912],
[-0.965096, 51.479467],
[-0.965022, 51.479839],
[-0.965119, 51.479864],
[-0.965158, 51.479934],
[-0.965118, 51.479986],
[-0.965079, 51.480003],
[-0.964986, 51.480008],
[-0.964814, 51.480469],
[-0.964713, 51.480815],
[-0.964557, 51.481186],
[-0.964451, 51.481367],
[-0.964258, 51.481676],
[-0.964064, 51.481907],
[-0.963818, 51.482233],
[-0.963612, 51.482446],
[-0.963573, 51.482527],
[-0.96324, 51.482993],
[-0.963159, 51.48311],
[-0.962887, 51.483489],
[-0.962857, 51.483532],
[-0.96226, 51.484227],
[-0.961746, 51.484772],
[-0.961011, 51.485436],
[-0.960925, 51.485538],
[-0.96082, 51.485674],
[-0.960769, 51.485812],
[-0.960738, 51.485976],
[-0.960762, 51.486151],
[-0.961251, 51.487158],
[-0.961434, 51.487488],
[-0.96167, 51.488166],
[-0.961854, 51.488684],
[-0.961993, 51.489342],
[-0.962062, 51.489832],
[-0.962149, 51.490148],
[-0.962316, 51.490867],
[-0.962325, 51.491381],
[-0.962282, 51.491567],
[-0.962231, 51.491748],
[-0.96217, 51.491965],
[-0.962085, 51.492304],
[-0.962076, 51.492341],
[-0.962015, 51.492547],
[-0.961802, 51.493072],
[-0.961778, 51.493193],
[-0.961799, 51.493316],
[-0.961866, 51.493439],
[-0.96199, 51.493601],
[-0.96218, 51.493791],
[-0.96234, 51.494115],
[-0.962565, 51.494704],
[-0.962615, 51.495204],
[-0.962929, 51.496871],
[-0.962859, 51.497507],
[-0.962926, 51.497682],
[-0.963123, 51.497916],
[-0.964421, 51.499253],
[-0.96585, 51.500681],
[-0.966663, 51.501626],
[-0.967288, 51.502525],
[-0.967773, 51.503099],
[-0.968082, 51.503465],
[-0.968877, 51.504125],
[-0.969, 51.504328],
[-0.96923, 51.505209],
[-0.969477, 51.505723],
[-0.969946, 51.506552],
[-0.97018, 51.507038],
[-0.970572, 51.507741],
[-0.972136, 51.509531],
[-0.972489, 51.509931],
[-0.973353, 51.511054],
[-0.973992, 51.511698],
[-0.974502, 51.51219],
[-0.975008, 51.512735],
[-0.975396, 51.51324],
[-0.975488, 51.513549],
[-0.975637, 51.513796],
[-0.975904, 51.51439],
[-0.976127, 51.515143],
[-0.976142, 51.515628],
[-0.976017, 51.516151],
[-0.975951, 51.516542],
[-0.976083, 51.517055],
[-0.976272, 51.517364],
[-0.976652, 51.517803],
[-0.976871, 51.518064],
[-0.977353, 51.518578],
[-0.977735, 51.518957],
[-0.978557, 51.519604],
[-0.978976, 51.519926],
[-0.979293, 51.520135],
[-0.980062, 51.520598],
[-0.980346, 51.520777],
[-0.980959, 51.521197],
[-0.981076, 51.521267],
[-0.981425, 51.521577],
[-0.982123, 51.522394],
[-0.982215, 51.522535],
[-0.982766, 51.523339],
[-0.982914, 51.523611],
[-0.983119, 51.524126],
[-0.98333, 51.524697],
[-0.983417, 51.525096],
[-0.983417, 51.525294],
[-0.983361, 51.525515],
[-0.98309, 51.525821],
[-0.983052, 51.525912],
[-0.983048, 51.525992],
[-0.983214, 51.52668],
[-0.98347, 51.527402],
[-0.983393, 51.527656],
[-0.98328, 51.527861],
[-0.98277, 51.528217],
[-0.982295, 51.52838],
[-0.981776, 51.528428],
[-0.9815, 51.528425],
[-0.980607, 51.528258],
[-0.980339, 51.528296],
[-0.980178, 51.528349],
[-0.979622, 51.528985],
[-0.979406, 51.529371],
[-0.978562, 51.530836],
[-0.978141, 51.531561],
[-0.978097, 51.531639],
[-0.978074, 51.531684],
[-0.977992, 51.531848],
[-0.977874, 51.532151],
[-0.977837, 51.5326],
[-0.977931, 51.53339],
[-0.977923, 51.533511],
[-0.977868, 51.533762],
[-0.977783, 51.534007],
[-0.977526, 51.534407],
[-0.977265, 51.534751],
[-0.976541, 51.535509],
[-0.976246, 51.536015],
[-0.975944, 51.53659],
[-0.975658, 51.537245],
[-0.975534, 51.537593],
[-0.975511, 51.537939],
[-0.975524, 51.538038],
[-0.975569, 51.538183],
[-0.975702, 51.538398],
[-0.975914, 51.538589],
[-0.97644, 51.538949],
[-0.976975, 51.539451],
[-0.977464, 51.53998],
[-0.977888, 51.540327],
[-0.97927, 51.541029],
[-0.979831, 51.541362],
[-0.980671, 51.541903],
[-0.982882, 51.544079],
[-0.983273, 51.54458],
[-0.983721, 51.545256],
[-0.984199, 51.546793],
[-0.984946, 51.548412],
[-0.985792, 51.549352],
[-0.986007, 51.549586],
[-0.987456, 51.55037],
[-0.987609, 51.550452],
[-0.988711, 51.551494],
[-0.989115, 51.551934],
[-0.989445, 51.552194],
[-0.989752, 51.552453],
[-0.989975, 51.552704],
[-0.990111, 51.552898],
[-0.991014, 51.553596],
[-0.991137, 51.553694],
[-0.991166, 51.553719],
[-0.991346, 51.553925],
[-0.991418, 51.554202],
[-0.991357, 51.554577],
[-0.991206, 51.554925],
[-0.990485, 51.555952],
[-0.990293, 51.556315],
[-0.990265, 51.556546],
[-0.990225, 51.556987],
[-0.990172, 51.557252],
[-0.990058, 51.557546],
[-0.990194, 51.558173],
[-0.990214, 51.558372],
[-0.99017, 51.558925],
[-0.990201, 51.559533],
[-0.990151, 51.560307],
[-0.990019, 51.560919],
[-0.989754, 51.561696],
[-0.989585, 51.562155],
[-0.989427, 51.562572],
[-0.988892, 51.563583],
[-0.988685, 51.564092],
[-0.988628, 51.56415],
[-0.988167, 51.564612],
[-0.987848, 51.565053],
[-0.987674, 51.565394],
[-0.987548, 51.56572],
[-0.987179, 51.566119],
[-0.986778, 51.566738],
[-0.98557, 51.569348],
[-0.985099, 51.57021],
[-0.985055, 51.571435],
[-0.985071, 51.571503],
[-0.98526, 51.572168],
[-0.985341, 51.572457],
[-0.985647, 51.573297],
[-0.985876, 51.574011],
[-0.985818, 51.574186],
[-0.985912, 51.574247],
[-0.985897, 51.574333],
[-0.986258, 51.574537],
[-0.986691, 51.574741],
[-0.988183, 51.575306],
[-0.988642, 51.575455],
[-0.988991, 51.575546],
[-0.989525, 51.575624],
[-0.989602, 51.575931],
[-0.9896, 51.576007],
[-0.989554, 51.576235],
[-0.989443, 51.576538],
[-0.989434, 51.576644],
[-0.98947, 51.576761],
[-0.989546, 51.576841],
[-0.989691, 51.576937],
[-0.990467, 51.57738],
[-0.990603, 51.577466],
[-0.990786, 51.577619],
[-0.991027, 51.577799],
[-0.991348, 51.578082],
[-0.991489, 51.578203],
[-0.992001, 51.578548],
[-0.992105, 51.578655],
[-0.992164, 51.57875],
[-0.992305, 51.579126],
[-0.99233, 51.579267],
[-0.992307, 51.579893],
[-0.992345, 51.580109],
[-0.992443, 51.580292],
[-0.992511, 51.580362],
[-0.992749, 51.580597],
[-0.993186, 51.580954],
[-0.993255, 51.58103],
[-0.993448, 51.581262],
[-0.993821, 51.581625],
[-0.995268, 51.582773],
[-0.995458, 51.582951],
[-0.995764, 51.583273],
[-0.996321, 51.583923],
[-0.997323, 51.584811],
[-0.998217, 51.58565],
[-0.998907, 51.586513],
[-0.999603, 51.587242],
[-0.999766, 51.58759],
[-0.9999, 51.587973],
[-1.000022, 51.588186],
[-1.00021, 51.588396],
[-1.000897, 51.58905],
[-1.001156, 51.589329],
[-1.00125, 51.589486],
[-1.001531, 51.590061],
[-1.001766, 51.590642],
[-1.001862, 51.590787],
[-1.002188, 51.591129],
[-1.002493, 51.591547],
[-1.002615, 51.591757],
[-1.002722, 51.592008],
[-1.002775, 51.59227],
[-1.002776, 51.59295],
[-1.002808, 51.593524],
[-1.002843, 51.59375],
[-1.002925, 51.594009],
[-1.002943, 51.59407],
[-1.003, 51.594183],
[-1.003016, 51.594213],
[-1.003243, 51.594525],
[-1.003442, 51.594993],
[-1.003298, 51.596136],
[-1.003289, 51.596675],
[-1.003244, 51.59697],
[-1.00314, 51.597336],
[-1.002848, 51.598035],
[-1.002712, 51.598246],
[-1.001786, 51.599282],
[-1.001485, 51.599577],
[-1.000807, 51.600176],
[-1.000389, 51.600642],
[-1.000195, 51.600826],
[-1.000003, 51.600979],
[-0.999879, 51.601037],
[-0.999566, 51.601252],
[-0.998928, 51.601643],
[-0.998687, 51.601808],
[-0.998515, 51.601956],
[-0.998163, 51.602343],
[-0.997791, 51.602865],
[-0.997693, 51.603069],
[-0.997615, 51.603412],
[-0.997454, 51.604678],
[-0.997444, 51.604874],
[-0.997564, 51.605521],
[-0.997563, 51.605619],
[-0.997544, 51.605672],
[-0.997469, 51.605747],
[-0.997376, 51.605802],
[-0.99668, 51.606082],
[-0.995211, 51.606577],
[-0.994833, 51.606747],
[-0.994655, 51.606861],
[-0.994312, 51.607068],
[-0.994096, 51.607281],
[-0.993842, 51.6076],
[-0.993653, 51.607929],
[-0.993092, 51.608951],
[-0.992562, 51.609606],
[-0.991972, 51.610168],
[-0.991887, 51.610244],
[-0.991702, 51.610449],
[-0.991454, 51.610773],
[-0.991228, 51.611193],
[-0.991136, 51.611432],
[-0.990809, 51.612527],
[-0.990753, 51.612839],
[-0.990731, 51.613165],
[-0.990757, 51.613419],
[-0.990891, 51.613819],
[-0.99103, 51.614107],
[-0.991212, 51.614367],
[-0.991675, 51.614929],
[-0.991738, 51.61505],
[-0.99182, 51.615295],
[-0.991901, 51.615812],
[-0.991849, 51.616574],
[-0.991943, 51.61684],
[-0.992444, 51.617454],
[-0.993055, 51.618423],
[-0.993408, 51.619488],
[-0.993539, 51.620879],
[-0.99307, 51.62226],
[-0.992762, 51.623026],
[-0.99245, 51.623926],
[-0.992422, 51.624054],
[-0.992418, 51.624247],
[-0.99251, 51.62459],
[-0.992523, 51.624807],
[-0.99247, 51.625087],
[-0.992286, 51.625821],
[-0.992278, 51.626078],
[-0.992308, 51.62616],
[-0.99248, 51.62653],
[-0.993022, 51.627294],
[-0.993166, 51.627562],
[-0.993511, 51.629119],
[-0.993638, 51.62936],
[-0.99515, 51.630875],
[-0.995699, 51.63136],
[-0.996207, 51.631744],
[-0.996488, 51.631925],
[-0.997398, 51.632381],
[-0.997812, 51.632629],
[-0.998431, 51.633101],
[-0.999115, 51.63367],
[-0.999643, 51.634138],
[-1.000048, 51.634591],
[-1.000537, 51.635195],
[-1.001747, 51.63676],
[-1.002586, 51.637763],
[-1.003487, 51.638589],
[-1.003616, 51.638684],
[-1.003722, 51.638775],
[-1.004252, 51.639309],
[-1.004479, 51.639583],
[-1.004986, 51.640357],
[-1.005173, 51.640717],
[-1.005478, 51.641304],
[-1.005573, 51.641471],
[-1.005847, 51.641846],
[-1.006188, 51.642164],
[-1.006213, 51.642216],
[-1.006185, 51.642275],
[-1.005697, 51.642707],
[-1.005653, 51.642766],
[-1.00565, 51.642826],
[-1.00576, 51.642925],
[-1.006639, 51.643337],
[-1.006153, 51.643754],
[-1.006072, 51.643819],
[-1.00596, 51.6439],
[-1.005445, 51.644275],
[-1.005276, 51.644371],
[-1.005229, 51.644406],
[-1.004733, 51.644743],
[-1.004382, 51.645013],
[-1.004255, 51.645014],
[-1.004152, 51.645066],
[-1.003868, 51.645265],
[-1.003679, 51.645419],
[-1.003443, 51.645642],
[-1.002349, 51.646674],
[-1.001428, 51.647716],
[-1.000885, 51.648392],
[-1.000497, 51.648804],
[-0.998359, 51.650964],
[-0.997144, 51.652143],
[-0.995981, 51.653142],
[-0.995218, 51.653927],
[-0.994675, 51.654433],
[-0.993988, 51.654967],
[-0.993449, 51.655352],
[-0.9925, 51.656138],
[-0.991807, 51.656627],
[-0.991404, 51.656911],
[-0.991212, 51.657042],
[-0.991034, 51.657194],
[-0.990864, 51.657392],
[-0.990614, 51.657901],
[-0.990449, 51.658115],
[-0.990252, 51.658326],
[-0.989241, 51.659143],
[-0.989074, 51.659297],
[-0.988831, 51.659582],
[-0.988419, 51.660432],
[-0.988218, 51.660744],
[-0.987723, 51.661256],
[-0.987453, 51.661506],
[-0.987029, 51.66183],
[-0.986431, 51.662242],
[-0.985788, 51.662608],
[-0.985351, 51.662813],
[-0.984575, 51.663087],
[-0.983851, 51.663304],
[-0.983472, 51.663452],
[-0.983163, 51.663593],
[-0.982673, 51.663878],
[-0.981839, 51.664452],
[-0.980888, 51.665043],
[-0.979879, 51.665638],
[-0.978447, 51.666406],
[-0.978025, 51.666588],
[-0.976917, 51.66694],
[-0.976332, 51.667213],
[-0.973617, 51.668873],
[-0.973274, 51.669052],
[-0.972572, 51.669358],
[-0.972214, 51.669474],
[-0.971877, 51.669571],
[-0.971003, 51.669768],
[-0.970276, 51.669872],
[-0.96833, 51.670052],
[-0.967796, 51.670115],
[-0.967036, 51.670243],
[-0.966271, 51.670426],
[-0.965951, 51.670518],
[-0.96545, 51.670691],
[-0.963082, 51.671565],
[-0.962354, 51.671861],
[-0.961818, 51.672094],
[-0.961687, 51.672185],
[-0.961582, 51.672369],
[-0.961773, 51.672759],
[-0.96187, 51.673543],
[-0.961931, 51.673826],
[-0.962097, 51.674188],
[-0.962848, 51.675347],
[-0.963932, 51.676607],
[-0.964951, 51.677582],
[-0.965879, 51.678373],
[-0.966201, 51.678636],
[-0.968712, 51.680446],
[-0.970015, 51.681317],
[-0.972494, 51.683013],
[-0.973438, 51.683718],
[-0.974527, 51.68463],
[-0.976238, 51.686226],
[-0.977161, 51.687107],
[-0.977751, 51.687792],
[-0.978035, 51.688112],
[-0.97973, 51.689997],
[-0.981747, 51.692109],
[-0.983496, 51.693705],
[-0.985695, 51.695561],
[-0.988469, 51.697575],
[-0.991677, 51.69969],
[-0.994026, 51.701067],
[-0.995652, 51.701961],
[-0.998334, 51.703287],
[-1.000045, 51.704075],
[-1.001693, 51.704767],
[-1.002422, 51.705076],
[-1.004439, 51.705874],
[-1.007837, 51.707152],
[-1.009208, 51.707689],
[-1.011133, 51.70855],
[-1.012657, 51.709274],
[-1.01366, 51.70982],
[-1.014449, 51.710302],
[-1.016101, 51.711355],
[-1.018864, 51.713177],
[-1.019808, 51.713745],
[-1.02168, 51.714779],
[-1.022753, 51.71532],
[-1.024051, 51.715912],
[-1.0272, 51.717268],
[-1.0286, 51.717803],
[-1.031137, 51.718657],
[-1.033374, 51.719329],
[-1.035188, 51.719814],
[-1.038181, 51.720505],
[-1.040332, 51.720901],
[-1.042896, 51.721316],
[-1.047343, 51.721927],
[-1.048309, 51.722094],
[-1.050299, 51.722472],
[-1.051925, 51.722821],
[-1.054322, 51.723463],
[-1.055401, 51.723775],
[-1.057053, 51.724317],
[-1.058276, 51.724759],
[-1.059859, 51.725367],
[-1.061398, 51.726021],
[-1.069252, 51.72949],
[-1.071952, 51.730661],
[-1.072947, 51.731078],
[-1.074456, 51.731579],
[-1.07607, 51.732066],
[-1.077273, 51.732368],
[-1.082087, 51.73342],
[-1.083274, 51.733732],
[-1.084663, 51.734151],
[-1.086993, 51.734983],
[-1.088631, 51.735676],
[-1.09042, 51.736552],
[-1.092111, 51.737555],
[-1.093699, 51.738626],
[-1.095444, 51.739963],
[-1.097243, 51.741506],
[-1.099407, 51.743593],
[-1.099824, 51.744054],
[-1.100108, 51.744356],
[-1.100736, 51.745021],
[-1.100912, 51.74524],
[-1.10213, 51.746778],
[-1.102848, 51.747748],
[-1.103202, 51.748279],
[-1.104071, 51.749684],
[-1.104727, 51.750873],
[-1.105091, 51.75158],
[-1.105396, 51.752331],
[-1.105735, 51.75329],
[-1.105955, 51.75425],
[-1.106137, 51.755223],
[-1.106239, 51.756704],
[-1.106206, 51.757611],
[-1.106057, 51.758786],
[-1.105757, 51.760014],
[-1.105445, 51.760971],
[-1.10456, 51.763298],
[-1.103578, 51.765778],
[-1.102409, 51.768524],
[-1.10124, 51.771143],
[-1.100037, 51.77357],
[-1.099228, 51.775023],
[-1.098433, 51.776258],
[-1.097099, 51.778157],
[-1.095489, 51.780238],
[-1.09353, 51.782392],
[-1.092575, 51.783357],
[-1.091604, 51.78429],
[-1.088423, 51.787071],
[-1.087334, 51.788139],
[-1.086626, 51.789045],
[-1.086289, 51.789619],
[-1.086086, 51.790107],
[-1.085935, 51.790588],
[-1.085876, 51.791022],
[-1.085854, 51.791583],
[-1.085875, 51.791919],
[-1.085934, 51.79233],
[-1.086138, 51.792934],
[-1.086396, 51.793471],
[-1.086702, 51.793969],
[-1.087522, 51.795144],
[-1.088221, 51.796039],
[-1.097564, 51.80694],
[-1.098799, 51.808263],
[-1.099625, 51.809069],
[-1.100044, 51.809427],
[-1.102362, 51.811561],
[-1.102574, 51.811782],
[-1.102967, 51.81225],
[-1.103406, 51.81282],
[-1.103798, 51.813388],
[-1.104146, 51.813948],
[-1.104613, 51.81492],
[-1.104914, 51.815662],
[-1.105756, 51.817997],
[-1.106974, 51.820968],
[-1.107323, 51.821605],
[-1.107725, 51.822268],
[-1.108079, 51.822778],
[-1.108661, 51.823532],
[-1.109264, 51.824257],
[-1.109775, 51.824821],
[-1.110772, 51.825796],
[-1.111743, 51.826615],
[-1.112682, 51.827331],
[-1.113798, 51.82808],
[-1.115123, 51.828876],
[-1.116995, 51.829824],
[-1.119012, 51.830692],
[-1.121807, 51.831727],
[-1.125846, 51.833354],
[-1.1284, 51.834531],
[-1.129382, 51.835018],
[-1.131071, 51.835894],
[-1.136467, 51.838831],
[-1.138253, 51.839689],
[-1.140062, 51.840468],
[-1.142782, 51.841452],
[-1.144144, 51.841899],
[-1.146194, 51.842499],
[-1.148081, 51.842987],
[-1.149685, 51.843345],
[-1.151091, 51.843626],
[-1.155345, 51.844329],
[-1.157329, 51.844766],
[-1.160136, 51.845472],
[-1.16153, 51.845873],
[-1.162614, 51.846211],
[-1.164406, 51.846831],
[-1.166969, 51.847862],
[-1.167345, 51.848031],
[-1.168359, 51.848508],
[-1.169851, 51.849253],
[-1.171632, 51.850231],
[-1.172565, 51.850784],
[-1.173954, 51.851692],
[-1.174786, 51.852272],
[-1.175827, 51.853074],
[-1.177317, 51.854297],
[-1.178959, 51.855596],
[-1.17919, 51.855794],
[-1.179983, 51.856424],
[-1.182157, 51.857991],
[-1.184941, 51.859916],
[-1.185492, 51.860267],
[-1.18613, 51.860681],
[-1.187296, 51.861443],
[-1.18824, 51.86201],
[-1.189785, 51.862901],
[-1.193483, 51.864963],
[-1.195149, 51.865889],
[-1.19582, 51.866309],
[-1.196909, 51.867095],
[-1.197509, 51.8676],
[-1.198298, 51.868353],
[-1.198518, 51.868585],
[-1.198834, 51.868966],
[-1.199161, 51.86939],
[-1.199773, 51.870321],
[-1.200213, 51.871122],
[-1.200562, 51.872053],
[-1.200722, 51.872633],
[-1.200862, 51.873411],
[-1.20091, 51.874282],
[-1.2009, 51.874653],
[-1.200679, 51.876961],
[-1.200664, 51.877938],
[-1.200809, 51.878955],
[-1.200964, 51.879538],
[-1.201157, 51.880183],
[-1.201441, 51.880796],
[-1.201882, 51.881624],
[-1.202579, 51.88269],
[-1.203732, 51.884382],
[-1.204247, 51.88523],
[-1.204821, 51.88628],
[-1.205582, 51.888058],
[-1.20585, 51.888783],
[-1.206184, 51.889912],
[-1.206462, 51.891335],
[-1.206591, 51.892597],
[-1.206624, 51.893494],
[-1.206569, 51.895427],
[-1.206468, 51.896427],
[-1.206356, 51.897959],
[-1.206215, 51.900436],
[-1.206109, 51.903103],
[-1.205958, 51.905595],
[-1.205668, 51.908121],
[-1.205384, 51.909756],
[-1.205175, 51.910692],
[-1.203888, 51.915837],
[-1.20327, 51.918548],
[-1.202541, 51.923421],
[-1.202289, 51.925677],
[-1.202079, 51.928589],
[-1.202059, 51.929342],
[-1.201983, 51.931909],
[-1.201984, 51.933798],
[-1.202064, 51.936295],
[-1.202213, 51.938888],
[-1.202391, 51.94076],
[-1.20262, 51.942014],
[-1.203274, 51.94363],
[-1.20351, 51.94416],
[-1.203754, 51.944551],
[-1.203947, 51.944792],
[-1.204436, 51.945235],
[-1.204742, 51.94545],
[-1.205037, 51.94562],
[-1.205291, 51.945753],
[-1.206613, 51.946282],
[-1.207335, 51.946601],
[-1.20759, 51.946669],
[-1.207742, 51.946685],
[-1.207851, 51.946722],
[-1.20794, 51.946776],
[-1.208, 51.946843],
[-1.208026, 51.946918],
[-1.208016, 51.946998],
[-1.207968, 51.947073],
[-1.207887, 51.947136],
[-1.207779, 51.94718],
[-1.207654, 51.947204],
[-1.207524, 51.947203],
[-1.20727, 51.947249],
[-1.207104, 51.947313],
[-1.205562, 51.94814],
[-1.204784, 51.948558],
[-1.204647, 51.948682],
[-1.204628, 51.948762],
[-1.204647, 51.948879],
[-1.204661, 51.948952],
[-1.204636, 51.949035],
[-1.204583, 51.949082],
[-1.204493, 51.949125],
[-1.204288, 51.949235],
[-1.204197, 51.949315],
[-1.204145, 51.949398],
[-1.204122, 51.949525],
[-1.204604, 51.951369],
[-1.204686, 51.951462],
[-1.204784, 51.951516],
[-1.204983, 51.951645],
[-1.205039, 51.951759],
[-1.205019, 51.95187],
[-1.204948, 51.951957],
[-1.204839, 51.952027],
[-1.204667, 51.952139],
[-1.204581, 51.952261],
[-1.204537, 51.952374],
[-1.20326, 51.956391],
[-1.203052, 51.956889],
[-1.202758, 51.957458],
[-1.202723, 51.957572],
[-1.202727, 51.95774],
[-1.202798, 51.957828],
[-1.202818, 51.9579],
[-1.202804, 51.957981],
[-1.202756, 51.958056],
[-1.202668, 51.958125],
[-1.202575, 51.958167],
[-1.202439, 51.958197],
[-1.20229, 51.958306],
[-1.202206, 51.958462],
[-1.201768, 51.959524],
[-1.201557, 51.959977],
[-1.201256, 51.960502],
[-1.199131, 51.963346],
[-1.198218, 51.964617],
[-1.196887, 51.966362],
[-1.195633, 51.968085],
[-1.194116, 51.970079],
[-1.193357, 51.970938],
[-1.19192, 51.972365],
[-1.190883, 51.973295],
[-1.19, 51.974021],
[-1.188477, 51.975188],
[-1.187132, 51.976089],
[-1.185693, 51.976983],
[-1.182898, 51.978529],
[-1.178534, 51.98088],
[-1.176359, 51.982095],
[-1.175381, 51.982701],
[-1.174437, 51.983315],
[-1.173575, 51.983904],
[-1.172794, 51.984528],
[-1.172153, 51.985098],
[-1.170297, 51.987084],
[-1.168153, 51.989322],
[-1.167019, 51.990439],
[-1.166093, 51.991231],
[-1.164, 51.992957],
[-1.163606, 51.993321],
[-1.162297, 51.99479],
[-1.161367, 51.995918],
[-1.161303, 51.996036],
[-1.161295, 51.996139],
[-1.161341, 51.996271],
[-1.161397, 51.996308],
[-1.161464, 51.996388],
[-1.16148, 51.996487],
[-1.161457, 51.99655],
[-1.161399, 51.996618],
[-1.161291, 51.996969],
[-1.16119, 51.997632],
[-1.160641, 51.999227],
[-1.160022, 52.00022],
[-1.158752, 52.001828],
[-1.158473, 52.002388],
[-1.158341, 52.002892],
[-1.158277, 52.003394],
[-1.158266, 52.003912],
[-1.158323, 52.004334],
[-1.158539, 52.005074],
[-1.159304, 52.006602],
[-1.159583, 52.007437],
[-1.159652, 52.007823],
[-1.159677, 52.008347],
[-1.159635, 52.008837],
[-1.159546, 52.009239],
[-1.159481, 52.009492],
[-1.159307, 52.009934],
[-1.158943, 52.0105],
[-1.158577, 52.010994],
[-1.157961, 52.011698],
[-1.157718, 52.011926],
[-1.157619, 52.012045],
[-1.157595, 52.012183],
[-1.157622, 52.012249],
[-1.157589, 52.012371],
[-1.157459, 52.012465],
[-1.157304, 52.012503],
[-1.156948, 52.01269],
[-1.15521, 52.014059],
[-1.153996, 52.015152],
[-1.153342, 52.015935],
[-1.152775, 52.01675],
[-1.151386, 52.01945],
[-1.151012, 52.020084],
[-1.150567, 52.020543],
[-1.150056, 52.020998],
[-1.149997, 52.021115],
[-1.14999, 52.021203],
[-1.149993, 52.021217],
[-1.149928, 52.021378],
[-1.149801, 52.021468],
[-1.149782, 52.021475],
[-1.149631, 52.021508],
[-1.149512, 52.021509],
[-1.149264, 52.021459],
[-1.149232, 52.021441],
[-1.148935, 52.021353],
[-1.148548, 52.021295],
[-1.145639, 52.021149],
[-1.144941, 52.021134],
[-1.144291, 52.021147],
[-1.143705, 52.021181],
[-1.143132, 52.021234],
[-1.142131, 52.021382],
[-1.140996, 52.02163],
[-1.140204, 52.021856],
[-1.139494, 52.022108],
[-1.138938, 52.022334],
[-1.138396, 52.022594],
[-1.13756, 52.023067],
[-1.136907, 52.023517],
[-1.136345, 52.023998],
[-1.136025, 52.024299],
[-1.135625, 52.024731],
[-1.13456, 52.025998],
[-1.134519, 52.026186],
[-1.134554, 52.026251],
[-1.134555, 52.026322],
[-1.13452, 52.02639],
[-1.134453, 52.026447],
[-1.134361, 52.02649],
[-1.134264, 52.026511],
[-1.134034, 52.026663],
[-1.133885, 52.026795],
[-1.133692, 52.027051],
[-1.133483, 52.027383],
[-1.133312, 52.027716],
[-1.133174, 52.028057],
[-1.132982, 52.028718],
[-1.132909, 52.029202],
[-1.132913, 52.029828],
[-1.132989, 52.030375],
[-1.133213, 52.031186],
[-1.133349, 52.031659],
[-1.136164, 52.041293],
[-1.136304, 52.041455],
[-1.136411, 52.041507],
[-1.136509, 52.041538],
[-1.136624, 52.041603],
[-1.136702, 52.041686],
[-1.136736, 52.04178],
[-1.136723, 52.041875],
[-1.136635, 52.041991],
[-1.136449, 52.042086],
[-1.136333, 52.042111],
[-1.136197, 52.042118],
[-1.13595, 52.042161],
[-1.135689, 52.042238],
[-1.135445, 52.042349],
[-1.135108, 52.042549],
[-1.134655, 52.042876],
[-1.130842, 52.046248],
[-1.130064, 52.04686],
[-1.128586, 52.047912],
[-1.127179, 52.048755],
...],
'type': 'LineString'}}]}
# Calculate Isochrone
client = openrouteservice.Client(key=my_key)
# Define a location
location = [(start_long, start_lat)]
# Define range (in seconds)
ranges = [600]
# Calculate isochrone
isochrone = client.isochrones(locations=location,
profile='driving-car',
range_type='time',
range=ranges,
interval=600)
# Convert to GeoDataFrame
gdf_iso = gpd.GeoDataFrame.from_features(isochrone['features'])
gdf_iso = gdf_iso.set_crs('EPSG:4326')
gdf_iso.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
# plot
fig = plt.figure(figsize = (30,30))
ax = fig.add_subplot(121)
divider = make_axes_locatable(ax)
minx, miny, maxx, maxy = gdf_iso.total_bounds
x_buffer = (maxx - minx) * 0.08
y_buffer = (maxy - miny) * 0.08
minx -= x_buffer
miny -= y_buffer
maxx += x_buffer
maxy += y_buffer
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
gdf_iso.plot(ax = ax,
color = 'purple',
alpha = .2)
ax.axis('off')
# Add title
ax.set_title('Example isochrone map', fontdict={'fontsize': 20}, loc='center')
# # add the contextily basemap
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik, crs=gdf_iso.crs)
plt.savefig(path + 'Example_isochrone.png', dpi=300, bbox_inches='tight')
gdf.to_file("/path/filename.shp")